|
|
 |
|
|
|
|
TIP Keep the scope of variables limited and avoid global variables if at all possible. |
|
|
|
|
|
|
|
|
Because there are different locations in which you can declare variables, you need to know where to access the different variables. Table 6.3 shows how and where you declare a variable; it also provides a sample name, the scope, and the lifetime of that variable. |
|
|
|
|
| Table 6.3 Declaration of Variables. | | Declaration | Location | Example | Scope | Lifetime | | Global | BAS file | gstrName | Application | Application | | Public | BAS file | gstrName | Application | Application | | Private | Module | mstrName | Module | Module | | Dim | Module | mstrName | Module | Module | | Public | Form/class | FirstName | Form/Class | Module | | Dim | Procedure | strName | Procedure | Procedure |
|
|
|
|
|
|
In this table, you can see that Global and Public are synonymous when either is declared in a standard module. Private and Dim are synonymous when either is declared in any type of module. A public variable declared in a module can only be used when its referenced with that modules name, such as <modName>.<variableName>. A Dim statement is the only declaration you can use to declare local variables within a procedure. |
|
|
|
|
|
|
|
|
Public Variables in Standard Modules |
|
|
|
|
|
|
|
|
Publicly scoped variables must be created in a standard module. To create a standard module, you need to select Project, Add Module from the Visual Basic menu. This inserts a code module into your project. These code modules are where you place all Public variables and Public procedures (which well cover later). |
|
|
|
|
|